﻿/*

The MIT License

Copyright (c) 2010 Cartesian Analytics, Inc. 

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

using System;
using System.Collections.Generic;
using System.Linq;
using Pipra.Math;
using Pipra.Math.Geometry;

namespace Pipra.Gis.Transformation
{
    /// <summary>
    /// A 7 parameter Helmert Transformation.
    /// </summary>
    public class Helmert7Transformation :
        ITransformation<Point3>,
		IEquatable<Helmert7Transformation>,
        ICoordinateOperationInfo
	{

		private class Inverted :
			InvertedTransformationBase<Helmert7Transformation, Point3>,
			IEquatable<Inverted>
		{

			public Inverted(Helmert7Transformation core) : base(core)
			{
			    if(
                    0 == Core.M
                )
			    {
			        throw new ArgumentException("Core cannot be inverted.");
			    }
			}

			public override Point3 TransformValue(Point3 coord) {
				coord = new Point3(
					(coord.X - Core.D.X) / Core.M,
					(coord.Y - Core.D.Y) / Core.M,
					(coord.Z - Core.D.Z) / Core.M
				);
				return new Point3(
					(coord.Y * Core.R.Z) - (coord.Z * Core.R.Y) + coord.X,
					(coord.Z * Core.R.X) - (coord.X * Core.R.Z) + coord.Y,
					(coord.X * Core.R.Y) - (coord.Y * Core.R.X) + coord.Z
				);
			}

            public override int GetHashCode()
            {
                return Core.GetHashCode();
            }

            public bool Equals(Inverted other)
            {
                return !ReferenceEquals(null, other)
                    && Core.D.Equals(other.Core.D)
                    && Core.R.Equals(other.Core.R)
                    && Core.M == other.Core.M
                ;
            }

			public override bool Equals(object obj)
			{
			    return null != obj
			        && (
			            obj is Inverted && Equals(obj as Inverted)
			        )
			    ;
			}

        }

        /// <summary>
        /// Translation vector.
        /// </summary>
        public readonly Vector3 D;
        /// <summary>
        /// Rotation vector.
        /// </summary>
        public readonly Vector3 R;
        /// <summary>
        /// Scale factor.
        /// </summary>
        public readonly double M;

        /// <summary>
        /// Constructs a new Helmert 7 param transform.
        /// </summary>
        /// <param name="translationVector">The vector used to translate.</param>
        /// <param name="rotationVector">The vector containing rotation parameters.</param>
        /// <param name="scaleFactor">The scale factor.</param>
        public Helmert7Transformation(
			ICoordinateTriple<double> translationVector,
			ICoordinateTriple<double> rotationVector,
			double scaleFactor
		) {
			D = new Vector3(translationVector);
			R = new Vector3(rotationVector);
			M = scaleFactor;
		}

        public void TransformValue(ref Point3 coord)
        {
            coord = new Point3(
                ((coord.X + (coord.Z*R.Y) - (coord.Y*R.Z))*M) + D.X,
                ((coord.Y + (coord.X*R.Z) - (coord.Z*R.X))*M) + D.Y,
                ((coord.Z + (coord.Y*R.X) - (coord.X*R.Y))*M) + D.Z
            );
        }

        [CLSCompliant(false)]
		public Point3 TransformValue(Point3 coord) {
			return new Point3(
				((coord.X + (coord.Z * R.Y) - (coord.Y * R.Z)) * M) + D.X,
				((coord.Y + (coord.X * R.Z) - (coord.Z * R.X)) * M) + D.Y,
				((coord.Z + (coord.Y * R.X) - (coord.X * R.Y)) * M) + D.Z
			);
		}

        public void TransformValues(Point3[] values)
        {
            for (int i = 0; i < values.Length; i++)
            {
                TransformValue(ref values[i]);
            }
        }

        public IEnumerable<Point3> TransformValues(IEnumerable<Point3> values)
        {
            return values.Select(TransformValue);
        }

        public ITransformation<Point3> GetInverse()
        {
            return new Inverted(this);
        }

        ITransformation<Point3, Point3> ITransformation<Point3, Point3>.GetInverse()
        {
            return GetInverse();
        }

		public bool HasInverse {
			get { return 0 != M; }
		}

        ITransformation ITransformation.GetInverse()
        {
			return GetInverse();
		}

        public bool Equals(Helmert7Transformation other)
        {
			return !ReferenceEquals(null,other)
				&& D.Equals(other.D)
				&& R.Equals(other.R)
				&& M == other.M 
			;
		}

		public override bool Equals(object obj)
		{
		    return null != obj
		        && (
		            (obj is Helmert7Transformation && Equals(obj as Helmert7Transformation))
		        )
		    ;
		}

		public override int GetHashCode() {
			return D.GetHashCode() ^ R.GetHashCode();
		}

        public IEnumerable<INamedParameter> GetParameters()
        {
            return new INamedParameter[]
                {
                    new NamedParameter<double>("dx",D.X),
                    new NamedParameter<double>("dy",D.Y),
                    new NamedParameter<double>("dz",D.Z),
                    new NamedParameter<double>("rx",R.X),
                    new NamedParameter<double>("ry",R.Y),
                    new NamedParameter<double>("rz",R.Z),
                    new NamedParameter<double>("m",M)
                }
            ;
        }

        public string Name
        {
            get { return "Helmert 7 Parameter Transformation"; }
        }

    }
}
